package com.gemme.gemme.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.wode.ShaidanActivity;

import java.util.HashMap;
import java.util.List;

public class ProductShaidanView extends RelativeLayout {
    private String dataString;
    private Context context;
    private  TextView saidan_count;
    public ProductShaidanView(Context context) {
        this(context,null);
    }

    public ProductShaidanView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ProductShaidanView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    ProductShaidanSingleView singleView1;
    ProductShaidanSingleView singleView2;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.product_shaidan,this);

        singleView1 = findViewById(R.id.singleView1);
        singleView2 = findViewById(R.id.singleView2);
        saidan_count = findViewById(R.id.saidan_count);

        findViewById(R.id.tv_more).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(StrUtil.isNUll(dataString)){
                    Toast.makeText(context, "数据为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(context, ShaidanActivity.class);
                intent.putExtra("data",dataString);
                context.startActivity(intent);
            }
        });
    }

    public void initData(long id) {
//        id = 25;
        String url = DataManager.BaseUrl_weicheng + "mall/shopproductcomment/findComment?current=1&size=2&productId="
                + id;
        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(getContext()));
//        headMap.put(DataManager.AuthorizationKey,"Bearer 2af9350c-dad4-4e04-b5b9-6a931f9b7b0c");
        DataManager.toServer3(getContext(), url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {


            @Override
            public void onResponse(String response) {
                dataString = response;
                try{
                    ProShaidanBean proShaidanBean = JSON.parseObject(response, ProShaidanBean.class);
                    List<ProShaidanBean.DataBean.RecordsBean> records = proShaidanBean.getData().getRecords();
                    setData(records);
                    saidan_count.setText("买家晒单("+proShaidanBean.getData().getTotal()+")");
                    System.out.println();

                }catch (Exception e){
                    //todo 暂时去除空界面显示
//                    setNUllDataView(true);
                }
            }
        });
    }

    private void setData(List<ProShaidanBean.DataBean.RecordsBean> records) {
        try {
            for (int i = 0; i < records.size(); i++) {
                ProShaidanBean.DataBean.RecordsBean recordsBean = records.get(i);
                if(i == 0){
                    setSIngleData(singleView1,recordsBean);
                }else if(i == 1){

                }
            }
        }catch (Exception e){

        }
    }

    private void setSIngleData(ProductShaidanSingleView singleView, ProShaidanBean.DataBean.RecordsBean recordsBean) {
        try {
            singleView.setVisibility(VISIBLE);
            singleView.setData(recordsBean);
        }catch (Exception e){

        }
    }
}
